package com.now.printer.ui.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.gamerole.orcameralib.CameraActivity;
import com.msd.ocr.idcard.LibraryInitOCR;
import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.base.BaseFragment2;
import com.now.printer.ui.activity.FileBrowerManagerActivity;
import com.now.printer.ui.activity.FilePreviewAndPrintActivity;
import com.now.printer.ui.activity.HelpActivity;
import com.now.printer.ui.activity.HtmlPrintActivity;
import com.now.printer.ui.activity.PinTuActivity;
import com.now.printer.ui.activity.ScanIdCardActivity;
import com.now.printer.ui.activity.StickyNoteActivity;
import com.now.printer.utils.DbUtils;
import com.now.printer.utils.FileUtils;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;

import org.xutils.common.util.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;

/**
 * Created by xudong on 2021/5/15.
 */
public class HomeFragment extends BaseFragment2 implements View.OnClickListener {

    private ImageView ivWenjian;
    private ImageView ivTupiann;
    private ImageView ivPintu;
    private ImageView ivZhengjian;
    private LinearLayout rlDivFileBrower;
    private RelativeLayout rl_div_look_use_help;
//    private SimpleImageBanner sibSimpleUsage;

    public static final int SCAN_IDCARD_REQUEST = 1;

    @Override
    protected void onLazy() {

    }

    public int getLayoutId() {
        return R.layout.fragment_home_container;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DbUtils.initSystemFileDb(getActivity(), handler);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (ivWenjian != null){
                    ivWenjian.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
                            intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                            getActivity().startActivity(intent);
                        }
                    });
                }
                if (nbToolbar != null){
                    nbToolbar.setSearchClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("type", "0");//0是所有，1是文件浏览，2是图片
                            intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                            getActivity().startActivity(intent);

                        }
                    });

                }
                if (ivTupiann != null){
                    ivTupiann.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("type", "2");//0是所有，1是文件浏览，2是图片
                            intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                            getActivity().startActivity(intent);
                        }
                    });
                }
            }
        }
    };

    private static final int REQUEST_CODE = 1001;
    private String[] frontAndBackImagePath = new String[2];
    private String idCardFrontImgPath = "";
    private String idCardBackImgPath = "";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setToolbar() {
        nbToolbar.setCenterTitle(getString(R.string.app_name));
        nbToolbar.setShowSearch();
        nbToolbar.setToolbarLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BaseActivity2) getActivity()).openMenu();

            }
        });

        //拼图
        ivPintu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), PinTuActivity.class);
                getActivity().startActivity(intent);
            }
        });
        //证件照
        ivZhengjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //                Intent intent3 = new Intent(getActivity(), CameraActivity.class);
//                getActivity().startActivity(intent3);
                Intent intent5 = new Intent(getActivity(), com.gamerole.orcameralib.CameraActivity.class);
                idCardFrontImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator  + "正面照"  + ".png";
                frontAndBackImagePath[0] = idCardFrontImgPath;

                intent5.putExtra(com.gamerole.orcameralib.CameraActivity.KEY_OUTPUT_FILE_PATH,idCardFrontImgPath);
                intent5.putExtra(com.gamerole.orcameralib.CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                startActivityForResult(intent5, REQUEST_CODE);




                //便利贴
//                Intent intent = new Intent();
//                intent.setClass(getActivity(), StickyNoteActivity.class);
//                getActivity().startActivity(intent);
            }
        });
        if (ivTupiann != null){
            ivTupiann.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("type", "2");//0是所有，1是文件浏览，2是图片
                    intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                    getActivity().startActivity(intent);
                }
            });
        }
    }

    @Override
    public void doBusiness(Context mContext, View convertView) {
//        initRecyclerView();
        ivWenjian = convertView.findViewById(R.id.iv_wenjian);
        ivWenjian.setOnClickListener(this);
        ivTupiann = convertView.findViewById(R.id.iv_tupiann);
        ivPintu = convertView.findViewById(R.id.iv_pintu);
        ivZhengjian = convertView.findViewById(R.id.iv_zhengjian);

        rl_div_look_use_help = convertView.findViewById(R.id.rl_div_look_use_help);
        rl_div_look_use_help.setOnClickListener(this);

        rlDivFileBrower = convertView.findViewById(R.id.rl_div_file_brower);
        rlDivFileBrower.setOnClickListener(this);


//        sibSimpleUsage = (SimpleImageBanner) findViewById(R.id.sib_simple_usage);
//        sibSimpleUsa ge.setSource(DemoDataProvider.getBannerList()).setOnItemClickListener((view, item, position) -> XToastUtils.toast("headBanner position--->" + position)).startScroll();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setToolbar();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.iv_wenjian:
//                Intent intent = new Intent();
//                intent.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
//                intent.setClass(getActivity(), FileBrowerManagerActivity.class);
//                getActivity().startActivity(intent);
//                break;
//            case R.id.tv_html_print:
//                Intent intent2 = new Intent();
//                intent2.setClass(getActivity(), HtmlPrintActivity.class);
//                getActivity().startActivity(intent2);
//                break;
//            case R.id.btn_file_brower:
//                Intent intent1 = new Intent(getActivity(), FileBrowerManagerActivity.class);
//                intent1.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
//                getActivity().startActivity(intent1);
//                break;
//            case R.id.btn_print_record:
//                if (onTabListener != null) onTabListener.onSetTab();
//                break;

            case R.id.rl_div_file_brower:
                Intent intent_file_brower = new Intent();
                intent_file_brower.putExtra("type", "0");//0是所有，1是文件浏览，2是图片
                intent_file_brower.setClass(getActivity(), FileBrowerManagerActivity.class);
                getActivity().startActivity(intent_file_brower);
                break;
            case R.id.rl_div_look_use_help:
                Intent intent3 = new Intent(getActivity(), HelpActivity.class);
                startActivity(intent3);
                break;
        }
    }

    public interface OnTabListener {
        void onSetTab();
    }

    public OnTabListener onTabListener;

    public void setOnTabListener(OnTabListener onTabListener) {
        this.onTabListener = onTabListener;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.i("HomeFragment onResume运行中");

    }


    /**
     * 进行排序
     * @param pageInfoList
     * @return
     */
//    private List<PageInfo> sortPageInfo(List<PageInfo> pageInfoList) {
//        Collections.sort(pageInfoList, (o1, o2) -> o1.getClassPath().compareTo(o2.getClassPath()));
//        return pageInfoList;
//    }

//        private void initRecyclerView() {
//        WidgetUtils.initGridRecyclerView(mRecyclerView, 3, DensityUtils.dp2px(2));
//        WidgetItemAdapter mWidgetItemAdapter = new WidgetItemAdapter(sortPageInfo(getPageContents()));
//        mWidgetItemAdapter.setOnItemClickListener(this);
//        mRecyclerView.setAdapter(mWidgetItemAdapter);
//    }

//    @Override
//    @SingleClick
//    public void onItemClick(View itemView, PageInfo widgetInfo, int pos) {
//        if (widgetInfo != null) {
//            openNewPage(widgetInfo.getName());
//        }
//    }

//    @Override
//    public void onConfigurationChanged(@NonNull Configuration newConfig) {
//        //屏幕旋转时刷新一下title
//        super.onConfigurationChanged(newConfig);
//        ViewGroup root = (ViewGroup) getRootView();
//        if (root.getChildAt(0) instanceof TitleBar) {
//            root.removeViewAt(0);
//            initTitle();
//        }
//    }


//    @Override
//    protected TitleBar initTitle() {
//        TitleBar titleBar = super.initTitle();
//
//        titleBar.setLeftImageResource(R.drawable.ic_action_menu);
//        titleBar.setLeftClickListener(new View.OnClickListener() {
//            @Override
//            @SingleClick
//            public void onClick(View v) {
//                getContainer().openMenu();
//            }
//        });
//        titleBar.addAction(new TitleBar.ImageAction(R.drawable.icon_action_query) {
//            @Override
//            @SingleClick
//            public void performAction(View view) {
////                openNewPage(SearchComponentFragment.class);
//                initNext();
////                onPickDoc();
//            }
//
//            @Override
//            public int rightPadding() {
//                return DensityUtils.dp2px(10);
//            }
//        });
//        titleBar.addAction(new TitleBar.ImageAction(R.drawable.icon_action_about) {
//            @Override
//            @SingleClick
//            public void performAction(View view) {
////                openNewPage(AboutFragment.class);
//            }
//        });
//        return titleBar;
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null){
            String idCardFront = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);

            if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT)){
//                frontAndBackImagePath[0] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);

                Intent intent = new Intent(getActivity(), CameraActivity.class);
                idCardBackImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator + "反面照" + ".png";
                frontAndBackImagePath[1] = idCardBackImgPath;

                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,idCardBackImgPath);
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                startActivityForResult(intent, REQUEST_CODE);

            }else if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_BACK)){
//
//                frontAndBackImagePath[1] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);
                LogUtil.i("current type id card front 正面照地址： " + frontAndBackImagePath[0]);
                LogUtil.i("current type id card back 反面照地址：" + frontAndBackImagePath[1]);

                Intent intent = new Intent(getActivity(), FilePreviewAndPrintActivity.class);
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_ID_CARD_FRONT_BACK, frontAndBackImagePath);
//            ArrayList<String> pathList = data.getStringArrayListExtra(CameraActivity.INTENT_KEY_ID_CARD_FRONT_BACK);
                startActivity(intent);
            }
        }
    }


}